home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / StdIFmIO.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  4.8 KB  |  193 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        StdIFmIO.cpp
  3.  
  4.     Contains:    functions for read/writing standard IconFamily from/to storage units.
  5.                 This file is Macintosh-specific.
  6.  
  7.     Owned by:    Tantek Çelik, Jens Alfke
  8.  
  9.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     To Do:
  12.         1. Ensure that usage of functions to write substreams works (nil prop & val).
  13.     In Progress:
  14.         
  15. */
  16.  
  17.  
  18. #ifndef _EXCEPT_
  19. #include <Except.h>
  20. #endif
  21.  
  22. #ifndef _ODDEBUG_
  23. #include "ODDebug.h"
  24. #endif
  25.  
  26. #ifndef _ODMEMORY_
  27. #include <ODMemory.h>
  28. #endif
  29.  
  30. #ifndef SOM_ODStorageUnit_xh
  31. #include <StorageU.xh>
  32. #endif
  33.  
  34. #ifndef SOM_ODStorageUnitView_xh
  35. #include <SUView.xh>
  36. #endif
  37.  
  38. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  39. #include <StdTypes.xh>
  40. #endif
  41.  
  42. #ifndef _STDTYPIO_
  43. #include "StdTypIO.h"
  44. #endif
  45.  
  46. #ifndef _STORUTIL_
  47. #include <StorUtil.h>
  48. #endif
  49.  
  50. #ifndef _TEMPOBJ_
  51. #include "TempObj.h"
  52. #endif
  53.  
  54. #ifndef __ICONS__
  55. #include <Icons.h>
  56. #endif
  57.  
  58. //==============================================================================
  59. // Constants
  60. //==============================================================================
  61.  
  62. static const int kNIconTypes = 8;
  63. static const ResType    kIconType[kNIconTypes] 
  64.                     = {'ics#','ics4','ics8',     0,     0,'ICN#','icl4','icl8'};
  65. static const short        kIconSize[kNIconTypes]
  66.                     = {    64,   128,   256,   512,  1024,   256,   512,  1024};
  67.  
  68.  
  69. //==============================================================================
  70. // IconFamily
  71. //==============================================================================
  72.  
  73.  
  74. const ODULong kAllMacIconsMask = 0x00E7;        // 16x16 and 32x32 / 1,4,8 bits deep 
  75.  
  76. //    If there is no value at the passed in prop & val then kODNULL is returned.
  77. //    This is consistent with the other routines in StdTypIO which return pointers.
  78. //    Other StdTypIO routines which actually return the value gotten, WARN if no value
  79. //    is there.
  80. //    The reason is that there may be clients of ODIconFamily which more easily 
  81. //    handle the flow of control if it just returns kODNULL instead of THROWing. -Tantek
  82.  
  83. ODIconFamily
  84. ODGetIconFamilyProp(Environment* ev, ODStorageUnit* su, ODPropertyName prop, 
  85.                     ODValueType val, ODULong iconMask)
  86. {
  87.     if( val == kODIconFamily )
  88.         val = kODIconFamilyMac;        // If only kODIconFamily specified, use platform type
  89.         
  90.     if (ODSUExistsThenFocus(ev, su, prop, val))
  91.     {
  92.         iconMask &= kAllMacIconsMask;
  93.         
  94.         ODULong which;
  95.         ODSize pos = sizeof(which);
  96.         StorageUnitGetValue(su, ev, sizeof(which), &which);        
  97.             // Which icons have data?
  98.         
  99.         if( (which & iconMask) == 0 )
  100.             return kODNULL;
  101.         
  102.         ODIconFamily iconFamily;
  103.         THROW_IF_ERROR( NewIconSuite((Handle*)&iconFamily) );
  104.         
  105.         ODHandle h=kODNULL; ODVolatile(h);
  106.         TRY{
  107.             for( int i=0; i<kNIconTypes; i++ )
  108.                 if( which & (1<<i) ) {
  109.                     ODSize size = kIconSize[i];
  110.                     pos += size;
  111.                     if( iconMask & (1<<i) ) {            // Read icon:
  112.                         h = ODNewHandle(size);
  113.                         StorageUnitGetValue(su, ev,size, ODLockHandle(h));
  114.                         ODUnlockHandle(h);
  115.                         THROW_IF_ERROR( AddIconToSuite((Handle)h,(Handle)iconFamily,kIconType[i]) );
  116.                         h = kODNULL;
  117.                     } else
  118.                         su->SetOffset(ev,pos);            // Don't need this icon
  119.                 }
  120.         }CATCH_ALL{
  121.             ODDisposeHandle(h);
  122.             DisposeIconSuite((Handle)iconFamily,kODTrue);
  123.             RERAISE;
  124.         }ENDTRY
  125.         
  126.         return iconFamily;
  127.     }
  128.     else
  129.         return kODNULL;
  130. }
  131.  
  132.                         
  133. static const char* const kODIconFamilyBaseString = kODIconFamily ":";
  134.  
  135. void
  136. ODSetIconFamilyProp(Environment* ev, ODStorageUnit* su, ODPropertyName prop, 
  137.                     ODValueType val, ODIconFamily iconFamily,
  138.                     ODBoolean removeOtherPlatformIcons)
  139. {
  140.     ODSUForceFocus(ev, su, prop, val);
  141.  
  142.     ODBoolean wholeValue = (prop!=kODNULL || val!=kODNULL);
  143.     
  144.     // See which icons exist in the family:
  145.     ODULong which = 0;
  146.     Handle icon;
  147.     if( iconFamily != kODNULL )
  148.         for( int i=0; i<kNIconTypes; i++ )
  149.             if( kIconType[i] && GetIconFromSuite(&icon,(Handle)iconFamily,kIconType[i])==noErr && icon )
  150.                 which |= (1<<i);
  151.             
  152.     if( which==0 && wholeValue) 
  153.     {
  154.         // If there are no icons, remove the value:
  155.         su->Remove(ev);
  156.     }
  157.     else 
  158.     {
  159.         su->SetOffset(ev,0);
  160.         ODSize size = su->GetSize(ev);
  161.         su->DeleteValue(ev,size);
  162.         
  163.         StorageUnitSetValue(su, ev, sizeof(which), &which);
  164.         for( int i=0; i<kNIconTypes; i++ )
  165.             if( kIconType[i] && GetIconFromSuite(&icon,(Handle)iconFamily,kIconType[i])==noErr && icon ) 
  166.             {
  167.                 TRY{
  168.                     ODLockHandle((ODHandle)icon);
  169.                     StorageUnitSetValue(su, ev, kIconSize[i], (ODValue)(*icon));
  170.                 }CATCH_ALL{
  171.                     ODUnlockHandle((ODHandle)icon);
  172.                     RERAISE;
  173.                 }ENDTRY
  174.                 ODUnlockHandle((ODHandle)icon);
  175.             }
  176.     }
  177.     
  178.     if( removeOtherPlatformIcons ) {
  179.         ODULong nValues = su->CountValues(ev);
  180.         ODULong index = 1;
  181.         while( nValues-- != 0 ) {
  182.             su->Focus(ev,kODNULL,kODPosSame, kODNULL,0,index);    // Focus to value by index
  183.             TempODValueType type = su->GetType(ev);
  184.             if( strncmp(type,kODIconFamilyBaseString,strlen(kODIconFamilyBaseString)) == 0
  185.                             && strcmp(type,val) != 0 )
  186.                 su->Remove(ev);
  187.             else
  188.                 index++;
  189.         }
  190.     }
  191. }
  192.                             
  193.